/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Logico;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author LFBP
 */
public class LecturaEscrituraArchivos {
    
    private ArrayList<Coordenada> array;
    
    public LecturaEscrituraArchivos(ArrayList<Coordenada> array){
        
        this.array = array;
        
    }
    
    public boolean guardarInforma(){
        try {
            
            BufferedWriter out = new BufferedWriter(new FileWriter("coordenadas.txt"));
            for (int i = 0; i < array.size(); i++) {
                out.write(array.get(i).getCoordenadaX()+","+array.get(i).getCoordenadaY());
                out.newLine();
            }
            out.close();
            
            return true;
            
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "ERROR2");
            return false;
        }
        
    }
    
    
    
    public boolean leerInformacion(){
        try {
            BufferedReader in = new BufferedReader(new FileReader("coordenadas.txt"));
                        
            while(in.ready()) {
                
                String linea[] = (in.readLine()).split(",");
                Coordenada coor = new Coordenada(Float.parseFloat(linea[0]),Float.parseFloat(linea[1]));
                array.add(coor);
                
            }
            
            in.close();
            return true;
        } catch (IOException ex) {
            guardarInforma();
            
        } 
        
        return false;
    }
}
